package com.naza.rpc.discovery;

import com.alibaba.nacos.api.naming.pojo.Instance;
import com.naza.rpc.client.ConsClient;
import com.naza.rpc.client.ZookeeperClient;
import com.naza.rpc.model.ServiceInstance;
import com.naza.rpc.register.ZookeeperRegister;
import com.naza.rpc.util.InstanceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yl
 */
public class ZookeeperDiscovery implements Discovery {

    private static final Logger LOG = LoggerFactory.getLogger(ZookeeperRegister.class);

    ZookeeperClient client = ZookeeperClient.getINSTANCE();

    @Override
    public List<ServiceInstance> getAll(String serviceName) {
        try {
            List<ServiceInstance> instances = client.getAllInstance(serviceName);
            return instances;
        }catch (Exception e){
            LOG.error("error in get service [{}] all instances by zk.",serviceName,e);
        }
        return null;
    }

    @Override
    public void monitor(String serviceName,DiscoveryListener listener) {
        try {
            client.monitor(serviceName,listener);
        }catch (Exception e){
            LOG.error("error in monitor service [{}] instances by zk.",serviceName,e);
        }
    }
}
